﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Drawing;

namespace ClippingEditor
{
    /// <summary>
    /// Interaction logic for PictureManager.xaml
    /// </summary>
    public partial class PictureManager : UserControl
    {
        public PictureManager()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(PictureManager_Loaded);
        }

        void PictureManager_Loaded(object sender, RoutedEventArgs e)
        {
            this.lbx_Images.ItemsSource = Images;
        }


        private void Btn_AddImage_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
            dlg.Filter = "所有图片文件|*.png;*.jpg;*.jpeg;*.gif;*.bmp|PNG(*.png)|*.png|JPEG(*.jpg;*.jpeg)|*.jpg;*.jpeg|GIF(*.gif)|*.gif|BMP(*.bmp)|*.bmp";
            dlg.Multiselect = true;
            var result = dlg.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                foreach (var file in dlg.FileNames)
                {
                    try
                    {
                        Bitmap bitmap = new Bitmap(file);
                        this.images.Add(bitmap);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }

        private ObservableCollection<Bitmap> images = new ObservableCollection<Bitmap>();

        public ObservableCollection<Bitmap> Images
        {
            get { return images; }
        }

        private void img_Selected_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                Bitmap bitmap = this.lbx_Images.SelectedItem as Bitmap;
                int index = this.images.IndexOf(bitmap);
                Dlg_ImageEditor imgEditor = new Dlg_ImageEditor(bitmap, rtn =>
                {
                    this.images.Remove(bitmap);
                    this.images.Insert(index, rtn);
                });
                imgEditor.Text = "图片编辑";
                imgEditor.ShowDialog();
            }
        }

        private void menuItem_EditImg_Click(object sender, RoutedEventArgs e)
        {
            MenuItem mi = sender as MenuItem;
            if (mi == null)
                return;
            Bitmap img = mi.DataContext as Bitmap;
            if (img != null)
            {
                int index = this.images.IndexOf(img);
                Dlg_ImageEditor imgEditor = new Dlg_ImageEditor(img, rtn =>
                {
                    this.images.Remove(img);
                    this.images.Insert(index, rtn);
                });
                imgEditor.Text = "图片编辑";
                imgEditor.ShowDialog();
            
            }
        }

        private void menuItem_RemoveImg_Click(object sender, RoutedEventArgs e)
        {
            MenuItem mi = sender as MenuItem;
            if (mi == null)
                return;
            Bitmap img = mi.DataContext as Bitmap;
            if (img != null)
                this.images.Remove(img);
        }
    }
}
